cmake_minimum_required(VERSION 3.21)
project(MotorMaster)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS ON)

set(PROJECT_ROOT_DIR ${CMAKE_SOURCE_DIR})
set(MM_LOG_DIR ${PROJECT_ROOT_DIR}/log/MotorMaster)
set(MM_CSV_DIR ${PROJECT_ROOT_DIR}/output/MotorMaster)
set(MM_FUSE_TEMPERATURE 60)
message(STATUS "MotorMaster log path: ${MM_LOG_DIR}")
message(STATUS "MotorMaster CSV path: ${MM_CSV_DIR}")
message(STATUS "MotorMaster fuse temperature: ${MM_FUSE_TEMPERATURE}")


set(BUILD_CANPP_SHARED_LIBS ON)
set(COMPILE_CANPP_EXAMPLE OFF)
set(CANPP_DISABLE_LOG ON)

option(COMPILE_MM_EXAMPLE "" ON)
option(MM_DISABLE_LOG OFF)
#set(MM_DISABLE_LOG ON)

add_subdirectory(3rdparty/canpp)
add_subdirectory(3rdparty/matplotplusplus)

add_library(MotorMaster SHARED
        src/Logger.h
        include/DJI/M3508.h
        src/DJI/M3508.cpp
        src/templatesImpl.cpp
        include/DM/J4310.h
        src/DM/J4310.cpp
        src/Algorithm/PIDController.cpp
        include/Algorithm/PIDController.h
)
add_dependencies(MotorMaster canpp)

target_include_directories(MotorMaster PUBLIC 3rdparty/canpp/src)

target_include_directories(MotorMaster PRIVATE 3rdparty/canpp/3rdparty/Ring-Buffer)
target_include_directories(MotorMaster PRIVATE 3rdparty/canpp/3rdparty/bs_thread_pool/include)
target_include_directories(MotorMaster PUBLIC include)

target_compile_definitions(MotorMaster PRIVATE MM_LOG_DIR="${MM_LOG_DIR}")
target_compile_definitions(MotorMaster PRIVATE MM_CSV_DIR="${MM_CSV_DIR}")
target_compile_definitions(MotorMaster PRIVATE MM_FUSE_TEMPERATURE=${MM_FUSE_TEMPERATURE})
if (MM_DISABLE_LOG)
    target_compile_definitions(MotorMaster PUBLIC MM_DISABLE_LOG)
else ()
    target_include_directories(MotorMaster PUBLIC 3rdparty/canpp/3rdparty/BqLog/dist/dynamic_lib/include)
    target_link_libraries(MotorMaster PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/canpp/3rdparty/BqLog/dist/dynamic_lib/linux64/${CMAKE_BUILD_TYPE}/libBqLog.so)
endif ()

target_link_libraries(MotorMaster PRIVATE canpp)
target_link_libraries(MotorMaster PRIVATE Matplot++::matplot)

if (COMPILE_MM_EXAMPLE)
    add_executable(M3508_Example example/DJI/M3508.cpp)
    add_executable(J4310_Example example/DM/J4310.cpp)
    add_executable(PID_Example example/Algorithm/PID.cpp)
    add_executable(Matplot_Example example/Algorithm/matplot.cpp)
    target_link_libraries(M3508_Example MotorMaster)
    target_link_libraries(J4310_Example MotorMaster)
    target_link_libraries(PID_Example MotorMaster)
    target_link_libraries(Matplot_Example MotorMaster)
endif ()